//
//  DataController.m
//  Youhuiju
//
//  Created by liuovyue on 16/8/11.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "DataController.h"
#import "CellPersonData.h"
#import "CellHead.h"
#import "VerifiedController.h"
#import "ModifyPersonDataController.h"
#import "GenderController.h"
#import "YODatePicker.h"
#import "VertiePlaceViewController.h"


@interface DataController ()<UITableViewDelegate, UITableViewDataSource,nameDelegate,genderDelegate,YODatePickerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic, strong)UILabel *labelTitle;
@property (nonatomic, strong)UIButton *buttonReturn;
@property (nonatomic, strong)UIView *viewBar;

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *arr;
@property (nonatomic, strong)NSMutableArray *arrText;
@property (nonatomic, strong)NSIndexPath *indexPathRow;
@property (nonatomic, strong)CellPersonData *cell;

@property (nonatomic, strong)UILabel *labelTt;
@property (nonatomic, strong)UITextField *fit;
@property (nonatomic, strong)CellPersonData  *cellTWO;
@property (nonatomic, strong)CellPersonData  *cellThree;
@property (nonatomic, strong)CellPersonData  *cellFour;

@property (nonatomic, strong)UIView *viewTimeBac;

@property (nonatomic, strong) YODatePicker *datePicker;
@property (nonatomic, copy)NSString *strValue;
@property (nonatomic, strong)UIView *viewHeadImage;
@property (nonatomic, strong)CellHead *cellHead;
@property (nonatomic, strong)NSString *value;
@property (nonatomic, strong)NSDate *maxDate;
@property (nonatomic, strong)NSDate *minDate;
//更改头像
@property (nonatomic, strong)UIImagePickerController *PickerImage;
@end

@implementation DataController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.arr = @[@"用户名",@"昵称",@"性别",@"出生日期",@"实名认证"].mutableCopy;
    self.view.backgroundColor = [UIColor colorWithRed:0.902 green:0.902 blue:0.902 alpha:1.0];
    //self.arrText = @[@"YHJ-1234",@"1234",@"保密",@"20160101",@"立即认证"].mutableCopy;
    // Do any additional setup after loading the view from its nib.
    [self createNavigationBar];
    [self createTableView];
    self.strValue = @"2016年08月09日星期二";
    
    
}





#pragma mark --顶部导航栏
- (void)createNavigationBar{
    
    self.viewBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64)];
    self.viewBar.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.viewBar];
    
    
    
   
   
    
    
    self.labelTitle = [[UILabel alloc]init];
    self.labelTitle.text = @"个人资料";
    self.labelTitle.textAlignment = NSTextAlignmentCenter;
    
    self.labelTitle.font = [UIFont systemFontOfSize:18*FitW];
    [self.viewBar addSubview:self.labelTitle];
    
    [_labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        
        //make.top.equalTo(self.viewBar.mas_top);
        make.left.equalTo(self.viewBar.mas_left).offset(100*FitW);
        make.right.equalTo(self.viewBar.mas_right).offset(-100*FitW);
        make.bottom.equalTo(self.viewBar.mas_bottom).offset(-14*FitH);
        
        
        
    }];
    //返回按钮
    self.buttonReturn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.buttonReturn setImage:[UIImage imageNamed:@"zfanhuijian"] forState:UIControlStateNormal];
    [self.buttonReturn addTarget:self action:@selector(tuihuiAcion:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewBar addSubview:self.buttonReturn];
    
    [_buttonReturn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.viewBar.mas_left).offset(17*FitW);
        make.width.mas_equalTo(22*FitW);
        make.height.mas_equalTo(22*FitH);
        make.bottom.equalTo(self.viewBar.mas_bottom).offset(-13*FitH);
        
        
        
    }];
    
    
    
}

#pragma mark -- 创建 tableView 
- (void)createTableView {
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 84, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 346*FitH - 84*FitH) style:UITableViewStylePlain];
   
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.viewBar.mas_bottom).offset(20*FitH);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.height.mas_equalTo(326*FitH);
        
        
        
    }];
    _tableView.scrollEnabled = NO;
    [_tableView registerNib:[UINib nibWithNibName:@"CellPersonData" bundle:nil] forCellReuseIdentifier:@"CellPersonData"];
    [_tableView registerNib:[UINib nibWithNibName:@"CellHead" bundle:nil] forCellReuseIdentifier:@"CellHead"];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1+self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        
    
            self.cellHead = [tableView dequeueReusableCellWithIdentifier:@"CellHead"];
        [self.cellHead.buttonHeadImage addTarget:self action:@selector(addHeadImageAcion:) forControlEvents:UIControlEventTouchUpInside];
            self.cellHead.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            self.cellHead.textFieldHead.alpha = 0;
            self.cellHead.labelName.text = @"头像";
            
            return self.cellHead;
    }
    else if(indexPath.row == 1){
        
        CellPersonData  *cellOne = [tableView dequeueReusableCellWithIdentifier:@"CellPersonData"];
        
        cellOne.labelText.alpha = 1;
        cellOne.labelTextOne.alpha = 0 ;
        cellOne.labelTextTwo.alpha = 0;
        cellOne.labelTextThree.alpha = 0;
        cellOne.labelFour.alpha = 0;
        cellOne.labelName.text = [self.arr objectAtIndex:indexPath.row - 1];
        cellOne.labelText.text = @"YHJ-1234";
        
        return cellOne;
        
        
        
        
    }
   else if(indexPath.row == 2){
      
            

                self.cell = [tableView dequeueReusableCellWithIdentifier:@"CellPersonData"];
                
        self.cell.labelTextOne.alpha = 1;
        self.cell.labelText.alpha = 0;
       self.cell.labelTextTwo.alpha = 0;
       self.cell.labelTextThree.alpha = 0;
       self.cell.labelFour.alpha = 0;
                self.cell.labelName.text = [self.arr objectAtIndex:indexPath.row - 1];
                self.cell.labelTextOne.text = @"1234";
                self.cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                return self.cell;
                
            
   }else if (indexPath.row == 3 ){
       self.cellTWO = [tableView dequeueReusableCellWithIdentifier:@"CellPersonData"];
       
       self.cellTWO.labelText.alpha = 0;
       self.cellTWO.labelTextOne.alpha = 0 ;
       self.cellTWO.labelTextThree.alpha = 0;
       self.cellTWO.labelFour.alpha = 0;
       self.cellTWO.labelName.text = [self.arr objectAtIndex:indexPath.row - 1];
       self.cellTWO.labelTextTwo.text = @"女";
       self.cellTWO.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
       return self.cellTWO;

   }
   
   
   
   
   
   else if(indexPath.row == 4) {
      self.cellThree = [tableView dequeueReusableCellWithIdentifier:@"CellPersonData"];
     
        self.cellThree.labelText.alpha = 0;
        self.cellThree.labelTextOne.alpha = 0 ;
        self.cellThree.labelTextTwo.alpha = 0;
       
    self.cellThree.labelFour.alpha = 0;
        self.cellThree.labelName.text = [self.arr objectAtIndex:indexPath.row - 1];
   
        self.cellThree.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
       self.cellThree.labelTextThree.text = @"20160809";
        return self.cellThree;
   }else {
       
       self.cellFour = [tableView dequeueReusableCellWithIdentifier:@"CellPersonData"];
       self.cellFour.labelFour.text = @"立即认证";
       self.cellFour.labelText.alpha = 0;
       self.cellFour.labelTextOne.alpha = 0 ;
       self.cellFour.labelTextTwo.alpha = 0;
       self.cellFour.labelTextThree.alpha = 0;
       self.cellFour.labelFour.alpha = 0;
       self.cellFour.labelName.text = [self.arr objectAtIndex:indexPath.row - 1];
       
       self.cellFour.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
       return self.cellFour;
       
   }
    
    

    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 101*FitH;
    }else {
        return 45*FitH;

    }
}

//更换头像
- (void)addHeadImageAcion:(UIButton *)button {
    
    
    self.viewHeadImage = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    self.viewHeadImage.backgroundColor = [UIColor colorWithRed:0.7556 green:0.7556 blue:0.7556 alpha:0.714897629310345];
    [self.view addSubview:self.viewHeadImage];
    
    UIButton *buttonRemoveBack = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonRemoveBack addTarget:self action:@selector(removeButton:) forControlEvents:UIControlEventTouchUpInside];
    buttonRemoveBack.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    [self.viewHeadImage addSubview:buttonRemoveBack];
    
    
    
    UIView *viewBackground = [[UIView alloc]init];
    [buttonRemoveBack addSubview:viewBackground];
    viewBackground.backgroundColor = [UIColor blackColor];
    [viewBackground mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.viewHeadImage.mas_top).offset(200*FitH);
        make.right.equalTo(self.viewHeadImage.mas_right).offset(-60*FitW);
        make.left.equalTo(self.viewHeadImage.mas_left).offset(60*FitW);
        make.bottom.equalTo(self.viewHeadImage.mas_bottom).offset(-300*FitH);
    }];
    UILabel *labelHead = [[UILabel alloc]init];
    labelHead.text = @"修改头像";
    labelHead.font = [UIFont systemFontOfSize:20*FitW];
    [viewBackground addSubview:labelHead];
    labelHead.textColor = [UIColor whiteColor];
    [labelHead mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewBackground.mas_top).offset(10*FitH);
       // make.right.equalTo(self.viewHeadImage.mas_right).offset(-60*FitW);
        make.left.equalTo(viewBackground.mas_left).offset(15*FitW);
        //make.bottom.equalTo(self.viewHeadImage.mas_bottom).offset(-300*FitH);
    }];
    
    UIButton *buttonPhoto = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonPhoto setTitle:@"拍照上传" forState: UIControlStateNormal];
    [buttonPhoto addTarget:self action:@selector(uploadPictures:) forControlEvents:UIControlEventTouchUpInside];
    [viewBackground addSubview:buttonPhoto];
    buttonPhoto.titleLabel.font = [UIFont systemFontOfSize:20*FitW];

    buttonPhoto.backgroundColor = [UIColor whiteColor];
    buttonPhoto.tintColor = [UIColor blackColor];
    buttonPhoto.titleLabel.textAlignment= NSTextAlignmentLeft;
    [buttonPhoto mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewBackground.mas_top).offset(40*FitH);
        make.right.equalTo(viewBackground.mas_right);
        make.left.equalTo(viewBackground.mas_left);
        make.height.mas_equalTo(65*FitH);
    }];
    
    UIButton *buttonLocal = [UIButton buttonWithType:UIButtonTypeSystem];
    buttonLocal.titleLabel.textAlignment= NSTextAlignmentLeft;
    buttonLocal.titleLabel.font = [UIFont systemFontOfSize:20*FitW];
    [buttonLocal setTitle:@"本地上传" forState: UIControlStateNormal];
    [buttonLocal addTarget:self action:@selector(uploadLocal:) forControlEvents:UIControlEventTouchUpInside];
    [viewBackground addSubview:buttonLocal];
    buttonLocal.backgroundColor = [UIColor whiteColor];
    buttonLocal.tintColor = [UIColor blackColor];
    [buttonLocal mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewBackground.mas_top).offset(106*FitH);
        make.right.equalTo(viewBackground.mas_right);
        make.left.equalTo(viewBackground.mas_left);
        make.height.mas_equalTo(65*FitH);
    }];

    
    
    
}
#pragma mark- 点击背景回到之前界面
- (void)removeButton:(UIButton *)button {
    [self.viewHeadImage removeFromSuperview];
}


#pragma mark - 拍照上传
- (void)uploadPictures:(UIButton *)button {
     self.PickerImage = [[UIImagePickerController alloc]init];
    self.PickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;

    self.PickerImage.allowsEditing = YES;
    
    self.PickerImage.delegate = self;
    
    [self presentViewController:self.PickerImage animated:YES completion:nil];

    
    
    [self.viewHeadImage removeFromSuperview];
}

#pragma mark - 本地上传照片
- (void)uploadLocal:(UIButton *)button {
    
    
     self.PickerImage = [[UIImagePickerController alloc]init];
    self.PickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    self.PickerImage.allowsEditing = YES;
    
    self.PickerImage.delegate = self;
    [self presentViewController:self.PickerImage animated:YES completion:nil];
    [self.viewHeadImage removeFromSuperview];

    
}
#pragma mark - 更换头像代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    
    self.cellHead.imageHead.image = newPhoto;
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    switch (indexPath.row) {
        
            case 2:
        {
            
            self.indexPathRow = indexPath;
           
            ModifyPersonDataController *name = [[ModifyPersonDataController alloc]init];
            name.delegate = self;
            [self.navigationController pushViewController:name animated:YES];
        }
            break;
            case 3:
        {
            
            GenderController *gender = [[GenderController alloc]init];
            gender.delegate = self;
            [self.navigationController pushViewController:gender animated:YES];
            
            
        }
            break;
            case 4:
        {
            

            
            
            self.datePicker = [[YODatePicker alloc]initDatePickerMode:YODatePickerModeDate andAddToSuperView:self.view];
            
            self.datePicker.backgroundColor = [UIColor colorWithRed:0.4 green:1.0 blue:0.4 alpha:1.0];
            
            self.datePicker.delegate = self;
            self.datePicker.labelHead.text = self.strValue;
            
            self.datePicker.datePickerMode = YODatePickerModeDate;

            
            [self.datePicker setDate:[NSDate date] animated:YES];
            NSDateFormatter *dateFormatter = [NSDate shareDateFormatter];
            [dateFormatter setDateFormat:kDateFormatYYYYMMDD];
            

            NSDate *  senddate=[NSDate date];
            

            
            NSString *  locationString=[dateFormatter stringFromDate:senddate];
            
            
            self.maxDate = [dateFormatter dateFromString:locationString];
            self.minDate = [dateFormatter dateFromString:@"19110809"];
            
            self.datePicker.minimumDate = self.minDate;
            
            self.datePicker.maximumDate = self.maxDate;
           
      
            
           [self.datePicker show];
            
        }
            break;
        case 5:
        {
            VertiePlaceViewController *verified = [[VertiePlaceViewController alloc]initWithNibName:@"VertiePlaceViewController" bundle:nil];
            
            [self.navigationController pushViewController:verified animated:YES];
            
        }
            break;
            
        default:
            break;
    }
    
    
    
    
    
    
}
//昵称修改
- (void)passName:(UITextField *)field{
    
   
        self.cell.labelTextOne.text = field.text;
        
}
//修改性别
- (void)passGender:(UILabel *)label {
 
    
    self.cellTWO.labelTextTwo.text = label.text;
    
    
}







- (void)datePicker:(YODatePicker *)datePicker didCancel:(UIButton *)sender {
    
//    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"关闭日期选择" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil, nil];
//    [alertView show];
    
    [self.viewTimeBac removeFromSuperview];
}

- (void)datePicker:(YODatePicker *)datePicker didSelectedDate:(NSDate*)date {
    
   
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[NSLocale currentLocale]];
    
    NSDateFormatter *dateTwoFormatter = [[NSDateFormatter alloc] init];
    [dateTwoFormatter setLocale:[NSLocale currentLocale]];
    [dateTwoFormatter setDateFormat:@"yyyy年MM月dd日"];
    
    if (datePicker.datePickerMode == YODatePickerModeDate) {
        [dateFormatter setDateFormat:@"yyyyMMdd"];
    
    } else if (datePicker.datePickerMode == YODatePickerModeTime) {
        [dateFormatter setDateFormat:@"HH:mm:ss"];
    } else if (datePicker.datePickerMode == YODatePickerModeYearAndMonth){
        [dateFormatter setDateFormat:@"MM/yy"];
    } else {
        [dateFormatter setDateFormat:@"dd MMMM yyyy HH:mm:ss"];
    }

    self.value = [dateFormatter stringFromDate:date];
    self.cellThree.labelTextThree.text = self.value;
    
    NSString *strYear = [dateTwoFormatter stringFromDate:date];
    
    
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"Sunday", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Beijing"];
    
    [calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit = NSWeekdayCalendarUnit;
    
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:date];
    
  NSString *strWeak = [weekdays objectAtIndex:theComponents.weekday];
    self.strValue = [strYear stringByAppendingString:strWeak];
    


    
    [self.viewTimeBac removeFromSuperview];
}





- (void)tuihuiAcion:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
